Canopy Openess Index in R

Author

Edson Silva-Júnior

Packages

library(terra)
terra 1.8.50
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::extract() masks terra::extract()
✖ dplyr::filter()  masks stats::filter()
✖ dplyr::lag()     masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyterra)

Anexando pacote: 'tidyterra'

O seguinte objeto é mascarado por 'package:stats':

    filter
library(hemispheR)

Data

Importing

images <- paste0("cropped-images/imagem", 1:4, ".png")

images
[1] "cropped-images/imagem1.png" "cropped-images/imagem2.png"
[3] "cropped-images/imagem3.png" "cropped-images/imagem4.png"

Visualizing

visualizing_canopy <- function(x){
  
  raster_bi <- terra::rast(x)
  
  ggplots <- ggplot() +
    tidyterra::geom_spatraster_rgb(data = raster_bi) +
    scale_fill_continuous(na.value = "transparent") +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) 
  
  print(ggplots)
  
}

purrr::walk(images, visualizing_canopy)
<SpatRaster> resampled to 501264 cells.

<SpatRaster> resampled to 501264 cells.

<SpatRaster> resampled to 501264 cells.

<SpatRaster> resampled to 501264 cells.

Calculating Canopy Openess

Visualizing

canopy_visualizing <- function(x){ 
  
  analy <- stringr::str_glue("analysis for {x}") 
  
  file <- x  |>
    hemispheR::import_fisheye()  |>
    hemispheR::binarize_fisheye()

  ggplt <- ggplot() +
    tidyterra::geom_spatraster(data = file) +
    scale_fill_viridis_c(na.value = "transparent", breaks = seq(0, 1, 1)) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    labs(title = analy) +
    theme_bw()
  
  print(ggplt)
  
}

purrr::walk(images, canopy_visualizing)
It is a circular fisheye, where xc, yc and radius are 1485.5, 1485.5, 1483.5
<SpatRaster> resampled to 501264 cells.

It is a circular fisheye, where xc, yc and radius are 1499.5, 1499.5, 1497.5
<SpatRaster> resampled to 501264 cells.

It is a circular fisheye, where xc, yc and radius are 1499.5, 1499.5, 1497.5
<SpatRaster> resampled to 501264 cells.

It is a circular fisheye, where xc, yc and radius are 1500, 1500, 1498
<SpatRaster> resampled to 501264 cells.

Calculating

canopy_Openess <- function(x){
  
  stringr::str_glue("analysis for {x}") |> message()
  
  raster <- x |>
    hemispheR::import_fisheye() |>
    hemispheR::binarize_fisheye()
  
  values_0 <- raster[raster > 0] |> terra::ncell()
  
  values_all <- raster |> terra::ncell()
  
  result <- values_0 / values_all
  
  print(result)
  
}

purrr::walk(images, canopy_Openess)
analysis for cropped-images/imagem1.png
It is a circular fisheye, where xc, yc and radius are 1485.5, 1485.5, 1483.5
[1] 0.49774
analysis for cropped-images/imagem2.png
It is a circular fisheye, where xc, yc and radius are 1499.5, 1499.5, 1497.5
[1] 0.5004268
analysis for cropped-images/imagem3.png
It is a circular fisheye, where xc, yc and radius are 1499.5, 1499.5, 1497.5
[1] 0.2591377
analysis for cropped-images/imagem4.png
It is a circular fisheye, where xc, yc and radius are 1500, 1500, 1498
[1] 0.1944832